<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>使用jsencrypt执行OpenSSL的RSA加密，解密</title>
</head>
<!--引入jsencrypt.js-->
<script src="js/jsencrypt.min.js"></script>
<script type="text/javascript">

    // 生成RSA密钥对
    const rsa = new JSEncrypt();
    const keypair = rsa.getKey();

    // 获取公钥和私钥
    const publicKey = keypair.getPublicKey();
    const privateKey = keypair.getPrivateKey();

    console.log("公钥：", publicKey);
    console.log("私钥：", privateKey);

    // 实例化一个RSA对象
    const rsaInstance = new JSEncrypt();

    // 设置公钥和私钥
    rsaInstance.setPublicKey(publicKey);
    rsaInstance.setPrivateKey(privateKey);

    // 定义待加密的字符串
    const plaintext = "Hello, World!";

    // 加密字符串
    const encrypted = rsaInstance.encrypt(plaintext);
    console.log("加密后的密文：", encrypted);
    // 解密密文
    const decrypted = rsaInstance.decrypt(encrypted);
    console.log("解密后的明文：", decrypted);
</script>

</html>